package com.fly;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;

import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.AsyncClientHttpRequest;
import org.springframework.http.client.AsyncClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory;
import org.springframework.util.concurrent.ListenableFuture;

public class AsyncRequest
{
    public static void main(String[] args)
        throws URISyntaxException, IOException, InterruptedException, ExecutionException
    {
        AsyncClientHttpRequestFactory asyncFactory = new HttpComponentsAsyncClientHttpRequestFactory();
        URI uri = new URI("https://www.baidu.com");
        AsyncClientHttpRequest asynReq = asyncFactory.createAsyncRequest(uri, HttpMethod.GET);
        ListenableFuture<ClientHttpResponse> future = asynReq.executeAsync();
        ClientHttpResponse response = future.get();
        System.out.println(response.getStatusCode());
        System.out.println(IOUtils.toString(response.getBody(), "utf-8"));
    }
}